package com.zs.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.system.common.pojo.BisShopPost;
import com.system.common.pojo.BisShopUser;
import com.zs.service.BisShopPostService;
import com.zs.service.BisShopUserService;

@Controller
@RequestMapping("shopUser")
public class BisShopUserController {
	
	@Autowired
	private BisShopUserService bisShopUserService;
	@Autowired
	private BisShopPostService bisShopPostService;
	
	@RequestMapping("show")
	public String query(Model model,HttpServletRequest request,HttpSession session){
		Map<String, Object> map=MuhuQuery(request);
		String shopId=(String)session.getAttribute("shopId");
		if (!shopId.equals("")&&shopId!=null) {
			map.put("shopId", shopId);
		}else{
			BisShopUser user=(BisShopUser)session.getAttribute("shopUser");
			map.put("shopId", user.getShopId());
		}
		List<BisShopUser> bisShopList=bisShopUserService.queryBisShopUser(map);
		model.addAttribute("bisShopList", bisShopList);
		List<BisShopPost> bisShopPosts=bisShopPostService.queryBisShopPost();
		model.addAttribute("bisShopPosts", bisShopPosts);
		return "BisShopUser/ShowBisShopUser";
	}  
	
	private Map<String, Object> MuhuQuery(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		String bisaccount=request.getParameter("bisaccount");
		String bisname=request.getParameter("bisname");
		String postEmp=request.getParameter("postEmp");
		String workNumber=request.getParameter("workNumber");
		String phone=request.getParameter("phone");
		String stateDate=request.getParameter("stateDate");
		String endDate=request.getParameter("endDate");
		if (bisaccount!=null&&!bisaccount.equals("")) {
			map.put("bisaccount", bisaccount);
			request.setAttribute("bisaccount", bisaccount);
		}
		if (bisname!=null&&!bisname.equals("")) {
			map.put("bisname", bisname);
			request.setAttribute("bisname", bisname);
		}
		if (postEmp!=null&&!postEmp.equals("")) {
			map.put("postEmp", postEmp);
			request.setAttribute("postEmp", postEmp);
		}
		if (workNumber!=null&&!workNumber.equals("")) {
			map.put("workNumber", workNumber);
			request.setAttribute("workNumber", workNumber);
		}
		if (phone!=null&&!phone.equals("")) {
			map.put("phone", phone);
			request.setAttribute("phone", phone);
		}
		if (stateDate!=null&&!stateDate.equals("")) {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			try {
				map.put("stateDate", sdf.parse(stateDate));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("stateDate", stateDate);
		}
		if (endDate!=null&&!endDate.equals("")) {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			try {
				map.put("endDate", sdf.parse(endDate));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("endDate", endDate);
		}
		return map;
	}
}
